<?php

/**
 * This is the model class for table "doctor".
 *
 * The followings are the available columns in table 'doctor':
 * @property integer $idDoctor
 * @property string $cedula
 * @property string $nombre
 * @property string $apellido1
 * @property string $apellido2
 * @property string $telefono
 * @property string $direccion
 * @property integer $idCanton
 * @property string $correo
 * @property integer $idUsuario
 * @property string $Estado
 *
 * The followings are the available model relations:
 * @property Cita[] $citas
 * @property Cliente[] $clientes
 * @property Usuario $idUsuario0
 * @property Canton $idCanton0
 */
class Doctor extends CActiveRecord
{


	public function getFullName()
	{
		return $this->nombre.' '.$this->apellido1;
	}

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Doctor the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	* Returns the the PK or a doctor that is different from
	* the doctor id received through argument
	*/
	public static function getRandDocId($doctorFav){
		
		$totalDocs = Doctor::model()->findAll();
		
		do {
			$randNum= rand(1,sizeof($totalDocs));
		} while ($totalDocs[$randNum-1]->idDoctor==$doctorFav);
		
		return $totalDocs[$randNum-1]->idDoctor;
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'doctor';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('cedula, nombre, apellido1, apellido2, telefono, direccion, idCanton, Estado', 'required'),
			array('idCanton, idUsuario', 'numerical', 'integerOnly'=>true),
			array('cedula, nombre, apellido1, apellido2, telefono, correo', 'length', 'max'=>50),
			array('direccion', 'length', 'max'=>100),
			array('Estado', 'length', 'max'=>1),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idDoctor, cedula, nombre, apellido1, apellido2, telefono, direccion, idCanton, correo, idUsuario, Estado', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'relcitas' => array(self::HAS_MANY, 'Cita', 'idDoctor'),
			'relclientes' => array(self::HAS_MANY, 'Cliente', 'idDoctor'),
			'relusuario' => array(self::BELONGS_TO, 'Usuario', 'idUsuario'),
			'relcanton' => array(self::BELONGS_TO, 'Canton', 'idCanton'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idDoctor' => 'Id Doctor',
			'cedula' => 'Cedula',
			'nombre' => 'Nombre de Doctor',
			'apellido1' => 'Apellido1',
			'apellido2' => 'Apellido2',
			'telefono' => 'Telefono',
			'direccion' => 'Direccion',
			'idCanton' => 'Id Canton',
			'correo' => 'Correo',
			'idUsuario' => 'Id Usuario',
			'Estado' => 'Estado',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idDoctor',$this->idDoctor);
		$criteria->compare('cedula',$this->cedula,true);
		$criteria->compare('nombre',$this->nombre,true);
		$criteria->compare('apellido1',$this->apellido1,true);
		$criteria->compare('apellido2',$this->apellido2,true);
		$criteria->compare('telefono',$this->telefono,true);
		$criteria->compare('direccion',$this->direccion,true);
		$criteria->compare('idCanton',$this->idCanton);
		$criteria->compare('correo',$this->correo,true);
		$criteria->compare('idUsuario',$this->idUsuario);
		$criteria->compare('Estado',$this->Estado,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}